/**
 * @file Direction.cpp
 * @author BadFatCat0919 (543015378@qq.com)
 * @brief 移动方向类
 */

#include "Direction.h"
#include "Params.h"
#include <cmath>

/**
 * @brief 获得两点之间的方向
 * @param startX 起点横坐标
 * @param startY 起点纵坐标
 * @param endX 终点横坐标
 * @param endY 终点纵坐标
 * @return int 方向
 */
int Direction::GetDirection(int startX, int startY, int endX, int endY) {
    int x = endX - startX, y = endY - startY;
    if(y > 0) {
        if(x > 0) {
            return UP_RIGHT;
        } else if(x == 0) {
            return UP;
        } else { // x < 0
            return UP_LEFT;
        }
    } else if(y == 0) {
        if(x > 0) {
            return RIGHT;
        } else if(x == 0) {
            return NONE;
        } else { // x < 0
            return LEFT;
        }
    } else { // y < 0
        if(x > 0) {
            return DOWN_RIGHT;
        } else if(x == 0) {
            return DOWN;
        } else { // x < 0
            return DOWN_LEFT;
        }
    }
}

/**
 * @brief 获得两个方向间的差值
 * @param dir1 方向1
 * @param dir2 方向2
 * @return int n倍PI/4(5为无效)
 */
int Direction::GetDirectionDiff(int dir1, int dir2) {
    if(dir1 == INVALID || dir2 == INVALID) {
        return 5; // 有无效方向，则不存在差值
    }
    if(dir1 == NONE || dir2 == NONE) {
        return 1; // 规定原地方向到任何方向差值都为1
    }
    int diff = std::abs(dir1 - dir2);
    if(diff <= 4) {
        return diff;
    } else {
        return 8 - diff;
    }
}

/**
 * @brief 反转方向
 * 
 * @param dir 
 * @return int 
 */
int Direction::ReverseDirection(int dir) {
    switch (dir)
    {
    case UP:
        return DOWN;
        break;
    case DOWN:
        return UP;
        break;
    case LEFT:
        return RIGHT;
        break;
    case RIGHT:
        return LEFT;
        break;
    case UP_RIGHT:
        return DOWN_LEFT;
        break;
    case UP_LEFT:
        return DOWN_RIGHT;
        break;
    case DOWN_RIGHT:
        return UP_LEFT;
        break;
    case DOWN_LEFT:
        return UP_RIGHT;
        break;
    default:
        return 0; 
        break;
    }
}
